import { memo, forwardRef, useEffect, useState, useRef } from "react"
import { Form, message, Input, Select, Button } from 'antd'
import bus from "../../../../../utils/eventBus"
import LdDialog from "../../../../../components/LdDialog"
import './index.scss'
const DetailView = forwardRef((props: any, ref: any) => {
    const { dialog, close, refresh } = props
    const formRef = useRef<any>(null)
    const [loading, setLoading] = useState<boolean>(false)
    const secretOption = useRef<any[]>([
        {
            label: '公开',
            value: 1
        },
        {
            label: '内部',
            value: 2
        },
        {
            label: '秘密',
            value: 3
        },
        {
            label: '机密',
            value: 4
        },
        {
            label: '绝密',
            value: 5
        },
    ])
    const setUserAuthSecretConfigFinished = (res: any) => {
        if (res.code === 200) {
            message.success(res.msg)
            setTimeout(() => {
                close && close()
                refresh && refresh()
            }, 2000)
        } else {
            message.error(res.msg)
            setLoading(false)
        }
    }
    const handleClose = () => {
        close && close()
    }
    const handleSave = () => {
        formRef.current.validateFields().then((res: any) => {
            const { userSecret } = res
            let data: any = []
            userSecret.map((item: any) => {
                data.push({ secret: item })
            })
            const params = {
                permission: dialog.entity.permission,
                data
            }
            setLoading(true)
            window.chromePro.setUserAuthSecretConfig(JSON.stringify(params))
        })
    }
    useEffect(() => {
        bus.addListener('setUserAuthSecretConfigFinished', setUserAuthSecretConfigFinished)
        if (dialog.entity) {
            const { data, authSecretName } = dialog.entity
            let secret: any = []
            data.map((item: any) => {
                secret.push(item.secret)
            })
            formRef.current.setFieldsValue({
                authSecretName,
                userSecret: secret
            })
        }
    }, [])
    useEffect(() => {
        return () => {
            bus.removeListener('setUserAuthSecretConfigFinished', setUserAuthSecretConfigFinished)
        }
    }, [])
    return (
        <>
            <LdDialog open={true} handleClose={handleClose} title='编辑涉密等级'>
                <Form className="DetailView" ref={formRef} style={{ width: '80%', margin: '0px auto' }} preserve={false}>
                    <Form.Item
                        label="人员等级"
                        name="authSecretName"
                        rules={[{ required: true, message: '请输入人员等级' }]}
                    >
                        <Input placeholder="请输入人员等级" disabled></Input>
                    </Form.Item>
                    <Form.Item
                        label="授权密级"
                        name="userSecret"
                        rules={[{ required: true, message: '请选择授权密级' }]}
                    >
                        <Select
                            placeholder="请选择授权密级"
                            options={secretOption.current}
                            mode='multiple'
                        ></Select>
                    </Form.Item>
                </Form>
                <div className="footer">
                    <Button loading={loading} type="primary" onClick={handleSave} className="footer-btn">保存</Button>
                </div>
            </LdDialog>
        </>
    )
})
export default memo(DetailView)